#install packages
library(panelView)
library(zoo)
library(StaggeredSynthDiD)
library(synthdid)
library(fect)
library(ggplot2)

#prepare data
laws = read.csv("statelaws.csv")
turnout = read.csv("stateturnout.csv")
laws = laws[laws$State!="",]
results <- list()

for (i in 1:nrow(laws)) {
  for (j in 2:ncol(laws)) { 
    if (!is.na(laws[i, j])) {
      if (laws[i, j] == "X") {
        results <- c(results, list(data.frame(STATE = laws$State[i], YEAR = colnames(laws)[j], Law = TRUE)))
      }else {
        results <- c(results, list(data.frame(STATE = laws$State[i], YEAR = colnames(laws)[j], Law = FALSE)))
      }
    }else {
      results <- c(results, list(data.frame(STATE = laws$State[i], YEAR = colnames(laws)[j], Law = FALSE)))
    }
  }
}
laws <- do.call(rbind, results)
laws$YEAR <-substring(laws$YEAR,2,10000)

data = merge(laws,turnout,by=c("STATE","YEAR"))
data = data[data$YEAR != 1982&data$YEAR != 1980,]
data$Y = as.numeric(gsub(",","",data$VOTE_FOR_HIGHEST_OFFICE))/as.numeric(gsub(",","",data$VEP))*100

names(data)[names(data)=="Law"] <- "W"
names(data)[names(data)=="STATE"] <- "State"
names(data)[names(data)=="YEAR"] <- "Year"

data[data$State == "Texas"&data$Year > 2014,]$W =1
data[data$State == "North Dakota"&data$Year  == 2016,]$W =1
data[data$State == "Virginia"&data$Year == 2020,]$W =1




data$W = as.numeric(data$W)

#create early vs late adopting groups
data$group = as.numeric(data$State %in% c("Arizona", "Georgia", "Indiana", "Ohio")) + 2*as.numeric(data$State %in% c("Alabama", "Kansas", "Mississippi", "North Dakota", "Tennessee", "Virginia", "Wisconsin"))

#synthetic difference-in-differences requires these column names
data$Unit = data$State
data$Time = data$Year

all_sdid_rev = StaggeredSynthDiD(data,vcov="jack")
presidential_sdid_rev = StaggeredSynthDiD(data[as.numeric(data$Year)%%4 == 0,],vcov="jack")
midterm_sdid_rev = StaggeredSynthDiD(data[as.numeric(data$Year)%%4 == 2,],vcov="jack")
all_early_sdid_rev <- StaggeredSynthDiD( data[ data$group != 2,],vcov="jack")
presidential_early_sdid_rev <- StaggeredSynthDiD( data[as.numeric(data$Year)%%4 == 0& data$group != 2,],vcov="jack")
midterm_early_sdid_rev <- StaggeredSynthDiD( data[as.numeric(data$Year)%%4 == 2& data$group != 2,],vcov="jack")
all_late_sdid_rev <- StaggeredSynthDiD( data[ data$group != 1,],vcov="jack")
presidential_late_sdid_rev <- StaggeredSynthDiD( data[as.numeric(data$Year)%%4 == 0& data$group != 1,],vcov="jack")
midterm_late_sdid_rev <- StaggeredSynthDiD( data[as.numeric(data$Year)%%4 == 2& data$group != 1,],vcov="jack")

all_sdid_es_rev = StaggeredSynthDiD_es(data,vcov="jack")
presidential_sdid_es_rev = StaggeredSynthDiD_es(data[as.numeric(data$Year)%%4 == 0,],vcov="jack")
midterm_sdid_es_rev = StaggeredSynthDiD_es(data[as.numeric(data$Year)%%4 == 2,],vcov="jack")
all_early_sdid_es_rev <- StaggeredSynthDiD_es( data[data$group != 2,],vcov="jack")
presidential_early_sdid_es_rev <- StaggeredSynthDiD_es( data[as.numeric(data$Year)%%4 == 0& data$group != 2,],vcov="jack")
midterm_early_sdid_es_rev <- StaggeredSynthDiD_es( data[as.numeric(data$Year)%%4 == 2& data$group != 2,],vcov="jack")
all_late_sdid_es_rev <- StaggeredSynthDiD_es( data[ data$group != 1,],vcov="jack")
presidential_late_sdid_es_rev <- StaggeredSynthDiD_es( data[as.numeric(data$Year)%%4 == 0& data$group != 1,],vcov="jack")
midterm_late_sdid_es_rev <- StaggeredSynthDiD_es( data[as.numeric(data$Year)%%4 == 2& data$group != 1,],vcov="jack")
